﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class RecruitCatalogDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public RecruitCatalogDAL()
        {

        }
        public DataSet LoadCatalogs(Nullable<int> Sequence, Nullable<int> EmployeeType, Nullable<int> bgYear, Nullable<int> EntType)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(5);

                dbManager.AddParameters(0, "@ActiveStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", Sequence);
                dbManager.AddParameters(2, "@bgYear", bgYear);
                dbManager.AddParameters(3, "@EmployeeType", EmployeeType);
                dbManager.AddParameters(4, "@EntType", EntType);

                strSQL = " SELECT c.*"
                        + ", (select count(cat_dtls_seq) from hrm_rc_catalog_dtls"
                            + " where cat_seq = c.cat_seq"
                            + " and record_status = @ActiveStatus) AS APP_TOTAL"
                        + ", (select count(cat_dtls_seq) from hrm_rc_catalog_dtls"
                            + " where cat_seq = c.cat_seq"
                            + " and cat_status_seq = 2"
                            + " and record_status = @ActiveStatus) AS EMP_TOTAL"
                    + " FROM HRM_RC_CATALOGS c"
                    + " WHERE c.RECORD_STATUS = @ActiveStatus"
                        + (Sequence.HasValue ? " AND c.CAT_SEQ = @Sequence" : "")
                        + (bgYear.HasValue ? " AND c.BG_YEAR = @bgYear" : "")
                        + (EmployeeType.HasValue ? " AND c.EMP_TYPE_SEQ = @EmployeeType" : "");
                        //+ (EntType.HasValue ? " AND e.ENT_TYPE_SEQ = @EntType" : "");

                return dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public DataSet LoadCatalogDtls(Nullable<int> Sequence, Nullable<int> CatalogSeq, Nullable<int> CatStatus)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@ActiveStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", Sequence);
                dbManager.AddParameters(2, "@CatalogSeq", CatalogSeq);
                dbManager.AddParameters(3, "@CatStatus", CatStatus);

                strSQL = " SELECT d.* "
                        + ", CONCAT(np.PREFIX_NAME, e.FIRST_NAME, '  ', e.LAST_NAME) AS TMP_EMP_TEXT"
                        + ", e.BIRTH_DATE, e.CITIZEN, f.FIELD_POS_NAME, cs.CAT_STATUS_NAME"
                    + " FROM HRM_RC_CATALOG_DTLS d"
                    + " INNER JOIN HRM_RC_CATALOGS c ON d.CAT_SEQ = c.CAT_SEQ"
                    + " LEFT JOIN HRM_RC_CATALOG_STATUSES cs ON d.CAT_STATUS_SEQ = cs.CAT_STATUS_SEQ"
                    + " LEFT JOIN HRM_TMP_EMPLOYEES e ON d.TMP_EMP_SEQ = e.TMP_EMP_SEQ"
                    + " LEFT JOIN MAS_NAME_PREFIXES np ON e.NAME_PREFIX_SEQ = np.NAME_PREFIX_SEQ"
                    + " LEFT JOIN HRM_MP_FIELD_POSITIONS f ON d.FIELD_POS_SEQ = f.FIELD_POS_SEQ"
                    + " WHERE d.RECORD_STATUS = @ActiveStatus"
                        + (Sequence.HasValue ? " AND d.CAT_DTLS_SEQ = @Sequence" : "")
                        + (CatalogSeq.HasValue ? " AND d.CAT_SEQ = @CatalogSeq" : "")
                        + (CatStatus.HasValue ? " AND d.CAT_STATUS_SEQ = @CatStatus" : "")
                    + " ORDER BY d.CAT_NO";

                return dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public DataSet LoadCatalogDtlsStatus()
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@ActiveStatus", SystemInfo._ActiveRecordStatus);

                strSQL = " SELECT *"
                    + " FROM HRM_RC_CATALOG_STATUSES "
                    + " WHERE RECORD_STATUS = @ActiveStatus";

                return dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Insert(int bgYear, string Period, string Name, Nullable<int> EmployeeType, string Remarks, int CreatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@bgYear", bgYear);
                dbManager.AddParameters(1, "@Period", Period);
                dbManager.AddParameters(2, "@Name", Name);
                dbManager.AddParameters(3, "@EmployeeType", EmployeeType);
                dbManager.AddParameters(4, "@Remarks", Remarks);
                dbManager.AddParameters(5, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(6, "@CreatedBy", CreatedBy);
                dbManager.AddParameters(7, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_RC_CATALOGS(BG_YEAR, CAT_PERIOD"
                        + ", CAT_NAME, EMP_TYPE_SEQ, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@bgYear, @Period, @Name, @EmployeeType, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(int Sequence, int bgYear, string Period, string Name, Nullable<int> EmployeeType, string Remarks, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(8);

                dbManager.AddParameters(0, "@bgYear", bgYear);
                dbManager.AddParameters(1, "@Period", Period);
                dbManager.AddParameters(2, "@Name", Name);
                dbManager.AddParameters(3, "@EmployeeType", EmployeeType);
                dbManager.AddParameters(4, "@Remarks", Remarks);
                dbManager.AddParameters(5, "@Sequence", Sequence);
                dbManager.AddParameters(6, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(7, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_RC_CATALOGS"
                           + " SET BG_YEAR = @bgYear"
                                + ", CAT_PERIOD = @Period"
                                + ", CAT_NAME = @Name"
                                + ", EMP_TYPE_SEQ = @EmployeeType"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE CAT_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE HRM_RC_CATALOGS"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE CAT_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void InsertDtls(int CatalogSeq, int No, Nullable<int> CatStatusSeq, Nullable<int> TmpEmpSeq, Nullable<int> FieldPosSeq, string Remarks, int CreatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@CatalogSeq", CatalogSeq);
                dbManager.AddParameters(1, "@No", No);
                dbManager.AddParameters(2, "@CatStatusSeq", CatStatusSeq);
                dbManager.AddParameters(3, "@TmpEmpSeq", TmpEmpSeq);
                dbManager.AddParameters(4, "@FieldPosSeq", FieldPosSeq);
                dbManager.AddParameters(5, "@Remarks", Remarks);
                dbManager.AddParameters(6, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(7, "@CreatedBy", CreatedBy);
                dbManager.AddParameters(8, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_RC_CATALOG_DTLS(CAT_SEQ, CAT_NO, CAT_STATUS_SEQ, TMP_EMP_SEQ, FIELD_POS_SEQ"
                        + ", REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@CatalogSeq, @No, @CatStatusSeq, @TmpEmpSeq, @FieldPosSeq, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void UpdateDtls(int Sequence, int CatalogSeq, int No, Nullable<int> CatStatusSeq, Nullable<int> TmpEmpSeq, Nullable<int> FieldPosSeq, string Remarks, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(9);

                dbManager.AddParameters(0, "@CatalogSeq", CatalogSeq);
                dbManager.AddParameters(1, "@No", No);
                dbManager.AddParameters(2, "@CatStatusSeq", CatStatusSeq);
                dbManager.AddParameters(3, "@TmpEmpSeq", TmpEmpSeq);
                dbManager.AddParameters(4, "@FieldPosSeq", FieldPosSeq);
                dbManager.AddParameters(5, "@Remarks", Remarks);
                dbManager.AddParameters(6, "@Sequence", Sequence);
                dbManager.AddParameters(7, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(8, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_RC_CATALOG_DTLS"
                           + " SET CAT_SEQ = @CatalogSeq"
                                + ", CAT_NO = @No"
                                + ", CAT_STATUS_SEQ = @CatStatusSeq"
                                + ", TMP_EMP_SEQ = @TmpEmpSeq"
                                + ", FIELD_POS_SEQ = @FieldPosSeq"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE CAT_DTLS_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void DeleteDtls(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@record_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@updated_by", UpdatedBy);
                dbManager.AddParameters(3, "@updated_when", DateTime.Today);

                strSQL = "UPDATE HRM_RC_CATALOG_DTLS"
                    + " SET RECORD_STATUS = @record_status"
                        + ", UPDATED_BY = @updated_by"
                        + ", UPDATED_WHEN = @updated_when"
                    + " WHERE CAT_DTLS_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }   
    }
}
